﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace HITs
{
    public partial class Form1 : Form
    {
        //*************** Khai báo biến toàn cục    
        DanhSachCoPhieu danhSachCoPhieuFull = new DanhSachCoPhieu();
        DanhSachCoPhieu danhSachCoPhieu = new DanhSachCoPhieu();
        DanhSachCoPhieu danhSachCoPhieuTuongLai = new DanhSachCoPhieu();
        List<string> listTenCP;
        List<string> listCPtheodoi;
        List<List<string>> listRankingHubAuthorityPageRank = new List<List<string>>();

        List<string> listMaNganh_TenNganh;
        List<string> listNhomNganh;
        List<List<string>> listPhanNganh;
        //**************************************

        public Form1()
        {
            InitializeComponent();
            ///listRankingHubAuthorityPageRank = GetTotalRanking_HubAuthorityPageRank_DelayGia();
        }

        #region Hàm ưu tiên

        private void Form1_Load(object sender, EventArgs e)
        {
            rdbtt_HOSE.Checked = true;
            listTenCP = new List<string>();
            txtbx_load_batdau.Text = CoreFunctions.Xuly_LayNgayHienTaiChoBatdau();

            listCPtheodoi = new List<string>();
            listCPtheodoi = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath + @"\Data\CPTheoDoi.txt");

            //CoreFunctions.MR_LietKeFibonaci(100);

            #region Efficient Frontier
            List<string> listStock = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath
                + @"\HOSE\BackTest_MyPortfolio.txt");
            lstbx_DanhMuc.Items.Clear();
            for (int i = 0; i < listStock.Count; i++)
                if (listStock[i] == "" || listStock[i] == " " || listStock[i] == "\t")
                {
                    listStock.RemoveAt(i);
                    i--;
                }
                else
                {
                    listStock[i] = listStock[i].ToUpper().Trim();
                    listStock[i] = listStock[i].Replace("\t", "");
                    lstbx_DanhMuc.Items.Add(listStock[i]);
                }

            List<string> listCK = new List<string>();
            listCK = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath
               + @"\HOSE\Allocation_effFontier.txt");
            cmbx_Reward_ToiUu.Items.Clear();
            for (int i = 0; i < listCK.Count; i++)
                if (listCK[i] != "")
                {
                    string[] part = listCK[i].Split(new string[] { "\t", "  " }, StringSplitOptions.RemoveEmptyEntries);
                    cmbx_Reward_ToiUu.Items.Add(part[0]);
                }
            #endregion
        }

        /// <summary>
        ///  hàm sao chép từ btt_loadstocks_Click_1 (đã có chỉnh sửa so với hàm gốc)
        ///  Ưu điểm chỉ cần bỏ đường dẫn file vào để xử lý
        /// </summary>
        /// <param name="pathFile"></param>
        private void LoadStock(string pathFile)
        {
            #region Load File DataStock
            string ngayBatDau, ngayKetThuc;

            string path = pathFile;

            List<List<string>> listDataStock = CoreFunctions.Xuly_LayListNgayGiaoDich_OutFile(path);

            ngayBatDau = txtbx_load_batdau.Text;
            ngayKetThuc = txtbx_load_ketthuc.Text;
            int idLuive = int.Parse(txtbx_quangthoigian.Text);

            if (rdbtt_HOSE.Checked == true)
                CoreFunctions._pathHOSE_HNX = System.Windows.Forms.Application.StartupPath + @"\HOSE\";
            else
                CoreFunctions._pathHOSE_HNX = System.Windows.Forms.Application.StartupPath + @"\HNX\";

            ngayBatDau = CoreFunctions.Xuly_LayNgayGiaoDichGanNhat(ngayBatDau);
            if (rdbtt_luive.Checked == true) /// Chọn mốc lùi về
            {
                idLuive += CoreFunctions._listNgayGiaoDich.IndexOf(ngayBatDau);
                if (idLuive >= CoreFunctions._listNgayGiaoDich.Count)
                    idLuive = CoreFunctions._listNgayGiaoDich.Count - 1;
                ngayKetThuc = CoreFunctions._listNgayGiaoDich[idLuive];
            }
            else
                ngayKetThuc = CoreFunctions.Xuly_LayNgayGiaoDichGanNhat(ngayKetThuc);

            /// Cập nhật biến static ở CoreFunctions
            CoreFunctions._ngayBatDau = ngayBatDau;
            CoreFunctions._ngayKetThuc = ngayKetThuc;
            CoreFunctions._soNgayGiaoDich = CoreFunctions._listNgayGiaoDich.IndexOf(ngayKetThuc) - CoreFunctions._listNgayGiaoDich.IndexOf(ngayBatDau);
            
            danhSachCoPhieu = new DanhSachCoPhieu();
            danhSachCoPhieuFull = new DanhSachCoPhieu();

            danhSachCoPhieuFull.File_LoadDanhSachCoPhieu(listDataStock);

            danhSachCoPhieu.File_LoadDanhSachCoPhieu(listDataStock, ngayBatDau, ngayKetThuc);

            string ngayBatDauTuongLai, ngayKetThucTuongLai;                 /// danh sách cổ phiếu tương lai

            CoreFunctions.Xuly_TinhNgayTuongLai(ngayBatDau, 10, out ngayBatDauTuongLai, out ngayKetThucTuongLai);

            if (ngayKetThucTuongLai != ngayBatDauTuongLai)
            {
                danhSachCoPhieuTuongLai = new DanhSachCoPhieu();
                danhSachCoPhieuTuongLai.File_LoadDanhSachCoPhieu(listDataStock, ngayBatDauTuongLai, ngayKetThucTuongLai);
            }
            #endregion

            #region Load VNIndex
            string path_VNindex = System.Windows.Forms.Application.StartupPath + @"\Data\^vnindex.txt";
            List<List<string>> listVnIndex = CoreFunctions.Xuly_LayListNgayGiaoDich_OutFile(path_VNindex);

            danhSachCoPhieu.File_AddCoPhieu(listVnIndex);
            danhSachCoPhieuFull.File_AddCoPhieu(listVnIndex);

            ShareData.danhsachCoPhieu = danhSachCoPhieuFull;
            #endregion

            #region Loại bỏ các CP

            danhSachCoPhieu = danhSachCoPhieuFull.Xuly_LayDanhSachCoPhieu(0, 100);

            /// In giá đóng cửa gần nhất
            danhSachCoPhieu.File_WriteFileGiaThiTruong();
            //danhSachCoPhieu.Xuly_FilterCp(5, 20000, false);
            danhSachCoPhieu.Xuly_FilterCp(500000);
            danhSachCoPhieu.Xuly_CreateListPortfolio();
            danhSachCoPhieuFull.Xuly_CreateListPortfolio();
            #endregion

            #region Fill TabPage
            FillTabPage_XepHangTheoNgay();

            #endregion

        }

        private void btt_loadstocks_Click_1(object sender, EventArgs e)
        {
            #region Load File DataStock
            string ngayBatDau, ngayKetThuc, path = "";
            if (ckbx_autoLoad.Checked == false && openFile.ShowDialog() == DialogResult.OK)
                path = openFile.FileName;
            else if (ckbx_autoLoad.Checked == false)
                return;
            else if (ckbx_autoLoad.Checked == true)
                path = System.Windows.Forms.Application.StartupPath + @"\Data\MetaStock_HOSE.txt";

            List<List<string>> listDataStock = CoreFunctions.Xuly_LayListNgayGiaoDich_OutFile(path);
            //CoreFunctions.File_SplitAllDataStockToHOSE(path);            /// Chuyển dữ liệu từ all metastock sang dữ liệu HOSE (chỉ xài khi cập nhật dữ liệu)

            ngayBatDau = txtbx_load_batdau.Text;
            ngayKetThuc = txtbx_load_ketthuc.Text;
            int idLuive = int.Parse(txtbx_quangthoigian.Text);

            if (rdbtt_HOSE.Checked == true)
                CoreFunctions._pathHOSE_HNX = System.Windows.Forms.Application.StartupPath + @"\HOSE\";
            else
                CoreFunctions._pathHOSE_HNX = System.Windows.Forms.Application.StartupPath + @"\HNX\";

            ngayBatDau = CoreFunctions.Xuly_LayNgayGiaoDichGanNhat(ngayBatDau);
            if (rdbtt_luive.Checked == true) /// Chọn mốc lùi về
            {
                idLuive += CoreFunctions._listNgayGiaoDich.IndexOf(ngayBatDau);
                if (idLuive >= CoreFunctions._listNgayGiaoDich.Count)
                    idLuive = CoreFunctions._listNgayGiaoDich.Count - 1;
                ngayKetThuc = CoreFunctions._listNgayGiaoDich[idLuive];
            }
            else
                ngayKetThuc = CoreFunctions.Xuly_LayNgayGiaoDichGanNhat(ngayKetThuc);

            /// Cập nhật biến static ở CoreFunctions
            CoreFunctions._ngayBatDau = ngayBatDau;
            CoreFunctions._ngayKetThuc = ngayKetThuc;
            CoreFunctions._soNgayGiaoDich = CoreFunctions._listNgayGiaoDich.IndexOf(ngayKetThuc) - CoreFunctions._listNgayGiaoDich.IndexOf(ngayBatDau);

            DialogResult diaglogResult;
            if (ckbx_autoLoad.Checked == true)
                diaglogResult = DialogResult.Yes;
            else
                diaglogResult = MessageBox.Show("Khoảng thời gian chọn:\r\n -Bắt đầu:\t"
                    + CoreFunctions.Xuly_ChuyenChuoiDataStock(ngayBatDau) + "\r\n"
                    + "-Kết thúc:\t" + CoreFunctions.Xuly_ChuyenChuoiDataStock(ngayKetThuc)
                    , "Thong bao quang thoi gian Load File", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

            if (diaglogResult == DialogResult.Yes)
            {
                danhSachCoPhieu = new DanhSachCoPhieu();
                danhSachCoPhieuFull = new DanhSachCoPhieu();

                danhSachCoPhieuFull.File_LoadDanhSachCoPhieu(listDataStock);

                danhSachCoPhieu.File_LoadDanhSachCoPhieu(listDataStock, ngayBatDau, ngayKetThuc);

                string ngayBatDauTuongLai, ngayKetThucTuongLai;                 /// danh sách cổ phiếu tương lai

                CoreFunctions.Xuly_TinhNgayTuongLai(ngayBatDau, 10, out ngayBatDauTuongLai, out ngayKetThucTuongLai);

                if (ngayKetThucTuongLai != ngayBatDauTuongLai)
                {
                    danhSachCoPhieuTuongLai = new DanhSachCoPhieu();
                    danhSachCoPhieuTuongLai.File_LoadDanhSachCoPhieu(listDataStock, ngayBatDauTuongLai, ngayKetThucTuongLai);
                }
            }
            else
                return;
            #endregion

            #region Load VNIndex
            ///string path_VNindex = @"C:\Users\hpsc\Desktop\filemetastock\^vnindex.txt";

            string path_VNindex = System.Windows.Forms.Application.StartupPath + @"\Data\^vnindex.txt";

            //string path_VN30 = @"C:\Users\hpsc\Desktop\filemetastock\^vn30.txt";
            List<List<string>> listVnIndex = CoreFunctions.Xuly_LayListNgayGiaoDich_OutFile(path_VNindex);
            //List<List<string>> listVn30= CoreFunctions.Xuly_LayListNgayGiaoDich_OutFile(path_VN30);

            danhSachCoPhieu.File_AddCoPhieu(listVnIndex);
            danhSachCoPhieuFull.File_AddCoPhieu(listVnIndex);

            //danhSachCoPhieuFull.Testing_VNIndex_MFI_RSI(14, 20);
            //danhSachCoPhieu.File_AddCoPhieu(listVn30);
            //danhSachCoPhieuFull.File_AddCoPhieu(listVn30);

            danhSachCoPhieuFull.Xuly_LoaiBoKhongCoNgayGiaoDichMoiNhat_VNINDEX(150);
            ShareData.danhsachCoPhieu = danhSachCoPhieuFull;
            Load_Nganh();
            #endregion

            #region Loại bỏ các CP
            //danhSachCoPhieu.Xuly_LoaiBoCacCPKhongPhaiBig4();
            //danhSachCoPhieuFull.Xuly_LoaiBoCacCPKhongPhaiBig4();
            //danhSachCoPhieuTuongLai.Xuly_LoaiBoCacCPKhongPhaiBig4();
            //danhSachCoPhieuFull.Xuly_LoadListZscore();

            danhSachCoPhieu = danhSachCoPhieuFull.Xuly_LayDanhSachCoPhieu(0, 100);
            /// In giá đóng cửa gần nhất
            danhSachCoPhieu.File_WriteFileGiaThiTruong();
            //danhSachCoPhieu.Xuly_FilterCp(5, 20000, false);
            danhSachCoPhieu.Xuly_FilterCp(500000);
            danhSachCoPhieu.File_CapNhatMaNganh();
            danhSachCoPhieu.Xuly_CreateListPortfolio();
            danhSachCoPhieuFull.Xuly_CreateListPortfolio();
            #endregion

            #region Fill TabPage
            FillTabPage_XepHangTheoNgay();
            #endregion
        }

        private void Load_Nganh()
        {
            List<string> fileload = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath + @"\Data\Nganh_DanhSachPhanNganh.txt");
            listPhanNganh = new List<List<string>>();
            foreach (string list in fileload)
            {
                List<string> listCP = new List<string>();
                string[] part = list.Split(new string[] { "\t", " " }, StringSplitOptions.RemoveEmptyEntries);
                if (part.Length > 0)
                {
                    listCP.AddRange(part);
                    listPhanNganh.Add(listCP);
                }
            }

            List<string> listTenCp = ShareData.danhsachCoPhieu.DanhSachTenCoPhieu;
            for (int i = 0; i < listPhanNganh.Count; i++)
                for (int j = 0; j < listPhanNganh[i].Count; j++)
                    if (listTenCp.IndexOf(listPhanNganh[i][j]) < 0)
                    {
                        listPhanNganh[i].RemoveAt(j);
                        j--;
                    }

            listMaNganh_TenNganh = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath + @"\Data\Nganh_MaNganh_TenNganh.txt");
            listNhomNganh = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath + @"\Data\Nganh_TenNhomNganh.txt");
        }
        #endregion

        #region Hàm sự kiện của Form

        private void btt_splitdataHOSE_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                string path = openFile.FileName;

                List<List<string>> listDataStock = CoreFunctions.Xuly_LayListNgayGiaoDich_OutFile(path);
                CoreFunctions.File_SplitAllDataStockToHOSE(path);

                /// Sao chép file 
                File.Copy(System.Windows.Forms.Application.StartupPath + @"\Data\MetaStock_HOSE.txt"
                    , System.Windows.Forms.Application.StartupPath + @"\Data\MetaStock_HOSE_Backup.txt"
                    , true);

                #region Update dữ liệu HOSE
                path = System.Windows.Forms.Application.StartupPath + @"\Data\MetaStock_HOSE_TungNgay.txt";
                listDataStock = CoreFunctions.Xuly_LayListNgayGiaoDich_OutFile(path);

                string oldPath = System.Windows.Forms.Application.StartupPath + @"\Data\MetaStock_HOSE.txt";
                List<List<string>> listOldData = CoreFunctions.Xuly_LayListNgayGiaoDich_OutFile(oldPath);

                listOldData.AddRange(listDataStock);
                listOldData.Sort(CoreFunctions.Compare_ListListString);

                List<string> listContent = new List<string>();

                /// Xử lý update cho Cafef
                string text, title = "<Ticker>,<DTYYYYMMDD>,<Open>,<High>,<Low>,<Close>,<Volume>";
                foreach (List<string> listValues in listOldData)
                {
                    text = "";
                    foreach (string str in listValues)
                        text += str + ",";
                    text = text.Substring(0, text.Length - 1);
                    listContent.Add(text);
                }

                listContent.Insert(0, title);
                CoreFunctions.File_WriteAnyFile(System.Windows.Forms.Application.StartupPath + @"\Data\"
                    , "MetaStock_HOSE.txt", listContent);

                danhSachCoPhieu.File_CapNhatMaNganh();
                #endregion

                /// Load file dữ liệu các Stock lên
                LoadStock(System.Windows.Forms.Application.StartupPath + @"\Data\MetaStock_HOSE.txt");

                /// Tính toán file excel
                btt_Execute_Click(null, null);

                /// Tính tỉ lệ RSI tất cả các cổ phiếu
                danhSachCoPhieu.Testing_RSI_TatCaCacCP();
            }
        }

        private void btt_addnewday_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                string path = openFile.FileName;
                List<List<string>> listDataStock = CoreFunctions.Xuly_LayListNgayGiaoDich_OutFile(path);

                string oldPath = System.Windows.Forms.Application.StartupPath + @"\Data\MetaStock_HOSE.txt";
                List<List<string>> listOldData = CoreFunctions.Xuly_LayListNgayGiaoDich_OutFile(oldPath);

                listOldData.AddRange(listDataStock);
                listOldData.Sort(CoreFunctions.Compare_ListListString);

                List<string> listContent = new List<string>();

                /// Xử lý update cho Cafef
                string text, title = "<Ticker>,<DTYYYYMMDD>,<Open>,<High>,<Low>,<Close>,<Volume>";
                foreach (List<string> listValues in listOldData)
                {
                    text = "";
                    foreach (string str in listValues)
                        text += str + ",";
                    text = text.Substring(0, text.Length - 1);
                    listContent.Add(text);
                }

                listContent.Insert(0, title);
                CoreFunctions.File_WriteAnyFile(System.Windows.Forms.Application.StartupPath + @"\Data\"
                    , "MetaStock_HOSE.txt", listContent);
            }
        }

        private void lstbx_xephangtheongay_gia_SelectedIndexChanged(object sender, EventArgs e)
        {
            string text = lstbx_xephangtheongay_gia.SelectedItem.ToString();
            text = text.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries)[0];

            HienThiCoPhieu(text);        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Chart_Line chart = new Chart_Line();
            chart.ShowDialog();
        }

        private void cmbx_tukhoa_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lstbx_khoiluong_SelectedIndexChanged(object sender, EventArgs e)
        {

            string text = lstbx_xephangtheongay_khoiluong.SelectedItem.ToString();
            text = text.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries)[0];

            HienThiCoPhieu(text);
            //HienThiChart(text);
        }

        private void lstbx_chisophattrien_SelectedIndexChanged(object sender, EventArgs e)
        {


        }

        private void lstbx_hesodongdangTB_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lstbx_hesodongdangchitiet_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btt_hesodongdang_loadfilecu_Click(object sender, EventArgs e)
        {

        }

        private void btt_hesodongdangchuky_loadfilecu_Click(object sender, EventArgs e)
        {

        }

        private void btt_bosungVNINDEX_Click(object sender, EventArgs e)
        {

        }

        private void btt_hamtinhnangkhac_Click(object sender, EventArgs e)
        {

        }

        private void btt_LoadLientucCodinh_Click(object sender, EventArgs e)
        {

        }

        private void lstbx_dongdangchuky_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lstbx_dongdangchukychitiet_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lstbx_sapxepCK_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmbx_xephangtrungbinh_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmbx_hsdd_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmbx_hsdd_chitiet_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmbx_dongdangchuky_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmbx_dongdangchukychitiet_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void rdbtt_HOSE_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rdbtt_HNX_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btt_loadlientucchisocoban_Click(object sender, EventArgs e)
        {

        }

        private void cmbx_chucnangphu_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lstbx_ketqua_chisokethop_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btt_them_chisokethop_Click(object sender, EventArgs e)
        {

        }

        private void btt_xoa_chisokethop_Click(object sender, EventArgs e)
        {

        }

        private void btt_thucthi_chisokethop_Click(object sender, EventArgs e)
        {

        }

        private void cmbx_motsomaukethop_kephopchiso_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btt_xoahet_chisokethop_Click(object sender, EventArgs e)
        {

        }

        private void btt_capnhat_chisokethop_Click(object sender, EventArgs e)
        {

        }

        private void txtbx_timkiemmaCK_tongquat_TextChanged(object sender, EventArgs e)
        {
            string text = txtbx_timkiemmaCK_tongquat.Text;
            text = text.ToUpper().Trim();
            foreach (CoPhieu CP in danhSachCoPhieu.DanhSachCoPhieu_List)
            {
                for (int i = 0; i < lstbx_xephangtheongay_gia.Items.Count; i++)
                    if (lstbx_xephangtheongay_gia.Items[i].ToString().Contains(text))
                    {
                        lstbx_xephangtheongay_gia.SelectedIndex = i;
                        return;
                    }        
            }

        }

        private void rdbtt_chonmau_toiuuhoa_CheckedChanged(object sender, EventArgs e)
        {
        }

        private void rdbtt_kethop_toiuuhoa_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void lstbx_cacchiso_DoubleClick(object sender, System.EventArgs e)
        {

        }

        private void lstbx_chisocoban_toiuuhoa_DoubleClick(object sender, System.EventArgs e)
        {

        }

        private void btt_them_toiuuhoa_Click(object sender, EventArgs e)
        {

        }

        private void btt_capnhat_toiuuhoa_Click(object sender, EventArgs e)
        {

        }

        private void btt_xoa_toiuuhoa_Click(object sender, EventArgs e)
        {

        }

        private void btt_xoahet_toiuuhoa_Click(object sender, EventArgs e)
        {

        }

        private void btt_thucthi_toiuuhoa_Click(object sender, EventArgs e)
        {

        }

        private void btt_len_toiuuhoa_Click(object sender, EventArgs e)
        {

        }

        private void btt_xuong_toiuuhoa_Click(object sender, EventArgs e)
        {

        }

        private void lstbx_mau_toiuuhoa_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void rdbtt_QTcu_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rdbtt_QTchonloc_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rdbtt_QTmoi_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void ckbx_trangthaicu_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btt_cluster_thucthi_Click(object sender, EventArgs e)
        {
            List<List<string>> listCP = danhSachCoPhieu.Testing_MarkovCluster(1, float.Parse(txtbx_cluster_threshold.Text));
            Chart_Group group = new Chart_Group();
            group.ShowDialog();
        }

        private void lstbx_nhom_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lstbx_cophieutrongnhom_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        
        #endregion

        public void FillTabPage_XepHangTheoNgay()
        {
            DanhSachCoPhieu dsCP = danhSachCoPhieu;
            string txt = "";
            int id_rank = 1;

            dsCP.Sort_ThayDoiGiaTheoNgay_DES();
            lstbx_xephangtheongay_gia.Items.Clear();
            foreach (CoPhieu cp in dsCP.DanhSachCoPhieu_List)
            {
                txt = cp.TenCoPhieu + "\t" + cp.ThayDoiGiaTheoNgay + " %\t" + id_rank.ToString();
                lstbx_xephangtheongay_gia.Items.Add(txt);
                id_rank++;
            }

            id_rank = 1;
            dsCP.Sort_ThayDoiKhoiLuongTheoNgay_DES();
            lstbx_xephangtheongay_khoiluong.Items.Clear();
            foreach (CoPhieu cp in dsCP.DanhSachCoPhieu_List)
            {
                txt = cp.TenCoPhieu + "\t" + cp.ThayDoiKhoiLuongTheoNgay + " %\t" + id_rank.ToString();
                lstbx_xephangtheongay_khoiluong.Items.Add(txt);
                id_rank++;
            }

            dsCP.Sort_TenCoPhieu_ASC();
        }

        private void HienThiChart(string tenCP)
        {
            string text = tenCP;
            List<float> SMA = new List<float>();
            List<float> listGia = new List<float>();
            List<float> listRankingPageRank = new List<float>();
     

            foreach (CoPhieu CP in danhSachCoPhieu.DanhSachCoPhieu_List)
                if (CP.TenCoPhieu == text)
                {
                    SMA = Indicators.SMA(CP.DanhSachGiaDongCua, 20);
                    listGia = CP.DanhSachGiaDongCua;
                    break;
                }

            foreach (List<string> listStr in listRankingHubAuthorityPageRank)
            {
                if (listStr[0].CompareTo(tenCP) > 0)
                    break;
                else if (listStr[0].CompareTo(tenCP) == 0)
                {
                    listRankingPageRank.Insert(0, float.Parse(listStr[3]) / 10);
                }
            }

            for (int i = 0; i < SMA.Count - listRankingPageRank.Count; i++)
                listRankingPageRank.Add(1);

            List<float> listVolatility = CoreFunctions.MR_TinhListVolatility(SMA, 20);
            List<float> listTop = new List<float>();
            List<float> listBottom = new List<float>();

            for (int i = 0; i < listVolatility.Count; i++)
            {
                listTop.Add(SMA[i] + 2 * (listVolatility[i]));
                listBottom.Add(SMA[i] - 2 * (listVolatility[i]));
            }

            for (int i = listTop.Count; i < listGia.Count; i++)
            {
                listGia.RemoveAt(i);
                i--;
            }

            for (int i = listTop.Count; i < SMA.Count; i++)
            {
                SMA.RemoveAt(i);
                i--;
            }

            listGia = CoreFunctions.Xuly_DaoNguocList(listGia);
            SMA = CoreFunctions.Xuly_DaoNguocList(SMA);
            listTop = CoreFunctions.Xuly_DaoNguocList(listTop);
            listBottom = CoreFunctions.Xuly_DaoNguocList(listBottom);
            listRankingPageRank = CoreFunctions.Xuly_DaoNguocList(listRankingPageRank);

            chart1.Series[0].Points.DataBindY(listGia);
            //chart1.Series[1].Points.DataBindY(SMA);
            //chart1.Series[2].Points.DataBindY(listTop);
            //chart1.Series[3].Points.DataBindY(listBottom);
            chart1.Series[4].Points.DataBindY(listRankingPageRank);

            CoreFunctions._chart_listValues = new List<List<float>>();
            CoreFunctions._chart_listValues.Add(listGia);
            //CoreFunctions._chart_listValues.Add(SMA);
            //CoreFunctions._chart_listValues.Add(listTop);
            //CoreFunctions._chart_listValues.Add(listBottom);
            CoreFunctions._chart_listValues.Add(listRankingPageRank);
        }

        private void HienThiChart(List<string> listNamePath, List<List<float>> listPath)
        {
            chart1.Series.Clear();

            CoreFunctions._chart_listName = new List<string>();
            CoreFunctions._chart_listValues = new List<List<float>>();

            for (int i = 0; i < listNamePath.Count; i++)
            {
                chart1.Series.Add(listNamePath[i]);
                chart1.Series[i].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                chart1.Series[i].BorderWidth = 3;
                chart1.Series[i].Points.DataBindY(listPath[i]);

                CoreFunctions._chart_listName.Add(listNamePath[i]);
                CoreFunctions._chart_listValues.Add(listPath[i]);
            }
            
        }

        private void HienThiCoPhieu(string tenCP)
        {
            ///lstbx_xephangtheongay_gia
            for (int i = 0; i < lstbx_xephangtheongay_gia.Items.Count; i++)
                if (lstbx_xephangtheongay_gia.Items[i].ToString().Contains(tenCP) == true)
                {
                    lstbx_xephangtheongay_gia.SelectedIndex = i;
                    break;
                }            

            ///lstbx_xephangtheongay_khoiluong
            for (int i = 0; i < lstbx_xephangtheongay_khoiluong.Items.Count; i++)
                if (lstbx_xephangtheongay_khoiluong.Items[i].ToString().Contains(tenCP) == true)
                {
                    lstbx_xephangtheongay_khoiluong.SelectedIndex = i;
                    break;
                }        
   
            //// hiển thị ngành
            txtbx_link.Text = "";
            string txt_manganh = "";
            foreach (CoPhieu cp in this.danhSachCoPhieu.DanhSachCoPhieu_List)
                if (cp.TenCoPhieu == tenCP)
                {
                    txt_manganh = cp.MaNganh;
                    break;
                }
            if (txt_manganh != "")
            {
                string txt_nhom = "";

                if (listNhomNganh == null)
                {
                    listNhomNganh = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath + @"\Data\Nganh_TenNhomNganh.txt");
                }

                foreach (string nhom in listNhomNganh)
                    if (nhom[0] == txt_manganh[0])
                    {
                        txt_nhom = nhom.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries)[1];
                        break;
                    }
                string txt_nganh = "";
                int index_nganh = 0;
                for (int i = 0; i < listMaNganh_TenNganh.Count;  i++)
                    if (listMaNganh_TenNganh[i].Substring(0, 3) == txt_manganh)
                    {
                        txt_nganh = listMaNganh_TenNganh[i].Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries)[1];
                        index_nganh = i;
                        break;
                    }
              
                cmbx_xephangtheongay_sapxepCK.SelectedIndex = cmbx_xephangtheongay_sapxepCK.Items.Count-1;
                lstbx_xephangtheongay_sapxepCK.Items.Clear();
                List<string> listCPTheoNganh = listPhanNganh[index_nganh];
                float tb_nganh = 0f;
                foreach(string nameCP in listCPTheoNganh)
                    if (nameCP != tenCP)
                    {
                        foreach (object o in lstbx_xephangtheongay_gia.Items)
                            if (o.ToString().Contains(nameCP))
                            {
                                lstbx_xephangtheongay_sapxepCK.Items.Add(o.ToString());
                                tb_nganh += float.Parse(o.ToString().Split(new string[] { "\t", "%" }, StringSplitOptions.RemoveEmptyEntries)[1]);                            
                            }                   
                    }
                tb_nganh /= (listCPTheoNganh.Count);
                txtbx_link.Text = "Nhóm: " + txt_nhom + "\r\n" + "Ngành: " + txt_nganh + "\r\n" + "Tb: " + Math.Round(tb_nganh, 1) + "%";

                if (listCPtheodoi.Contains(tenCP))
                    ckbx_theodoi.Checked = true;
                else
                    ckbx_theodoi.Checked = false;
            }
        }

        private List<List<string>> GetTotalRanking_HubAuthorityPageRank_DelayGia()
        {    
            List<List<string>> content = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath + @"\HOSE\Testing_AllStock_HubAuthorityPageRank.txt"
                , false, new string[] { "\t" });
            for (int i = 0; i < content.Count; i++)
                if (content[i][1].CompareTo("20130226") < 0)
                {
                    content.RemoveAt(i);
                    i--;
                }
            return content;
        }

        private void btt_ChartMACD_Click(object sender, EventArgs e)
        {
            Chart_Line chart = new Chart_Line();
            chart.Show();
        }

        private void btt_Execute_Click(object sender, EventArgs e)
        {
            if (danhSachCoPhieu.DanhSachCoPhieu_List.Count != 0)
            {
                #region Testing
                #region * Testing Cắt lỗ chốt lời - T Cộng
                //List<Point> listCatLoChotLoi = new List<Point>();
                //listCatLoChotLoi.Add(new Point(8, 12));
                //listCatLoChotLoi.Add(new Point(4, 12));
                //listCatLoChotLoi.Add(new Point(2, 10));
                //listCatLoChotLoi.Add(new Point(2, 8));
                //listCatLoChotLoi.Add(new Point(2, 6));

                //danhSachCoPhieu.Testing_CatLoChotLoi_Function(1, 4, 3, listCatLoChotLoi, 1);
                //danhSachCoPhieu.Testing_CatLoChotLoi_Function(1, 4, 3, listCatLoChotLoi, 2);

                //List<int> listTcong = new List<int>(new int[] { 8, 40, 90 });
                //danhSachCoPhieu.Testing_TinhT_Cong(listTcong);
                #endregion

                #region * Testing Hub, Authority và PageRank
                ////danhSachCoPhieu.Testing_TinhHubScore(1, 6, 0.8f);                                       /// Tính theo tổng hợp
                ////danhSachCoPhieu.Testing_TinhHubScore(2, 6, 0.8f);                                       /// Tính theo khối lượng giao dịch
                ////danhSachCoPhieu.Testing_TinhHubScore(3, 6, 0.8f);                                       /// Tính theo giá trị giao dịch
                ////danhSachCoPhieu.Testing_TinhHubScore_Chuky(1, 6, 0.8f, 3);

                ////List<float> listHub = new List<float>();
                ////List<float> listAuthority = new List<float>();
                ////List<float> listPageRank = new List<float>();
                ////danhSachCoPhieu.Testing_TinhHubAuthorityScore_Chuky(1, 6, 0.8f, 3, ref listHub, ref listAuthority);
                ////danhSachCoPhieu.Testing_TinhHubAuthorityScore_Delay(1, 6, 0.8f, 60, 30, ref listHub, ref listAuthority);
                ////danhSachCoPhieu.Testing_TinhPageRank_Delay(1, 6, 0.8f, 60, 30, ref listPageRank);
                ////danhSachCoPhieu.Testing_TinhHubAuthorityPageRank_Delay(1, 6, 0.9f, 60, 30, ref listHub, ref listAuthority, ref listPageRank);
                ////danhSachCoPhieu.Testing_TinhHubAuthorityPageRank_Delay(2, 6, 0.9f, 60, 30, ref listHub, ref listAuthority, ref listPageRank);
                ////danhSachCoPhieu.Testing_TinhHubAuthorityPageRank_Delay(3, 6, 0.9f, 60, 30, ref listHub, ref listAuthority, ref listPageRank);

                //danhSachCoPhieu.Testing_TinhHubAuthorityPageRank_Delay_Gia_KhoiLuong(6, 0.9f, 60, 30);
                #endregion

                #region * Testing Tính Xu hướng
                //List<List<float>> listXuHuong = danhSachCoPhieu.Xuly_TinhXuHuongTungNgayMoiCP(out listTenCP);
                //danhSachCoPhieuTuongLai.Testing_XuHuong_KiemTraTuongLai(listXuHuong, listTenCP);
                #endregion

                #region * Testing Markov Cluster
                //List<List<string>> listCluster = danhSachCoPhieu.Testing_MarkovCluster(1, 0.97f);

                //if (danhSachCoPhieuTuongLai.DanhSachCoPhieu_List.Count != 0)
                //    danhSachCoPhieuTuongLai.Testing_MarkovCluster_KiemTraTuongLaiSimilarCosine_Gia(listCluster);
                #endregion

                #region * Testing Tìm kiếm giống nhau ở quá khứ
                //listTenCP = new List<string>();
                //List<List<float>> listHeSo = new List<List<float>>();

                //listTenCP.AddRange(new string[] { "MSN" });
                //int[] arrChuky = new int[] { 1, 3 };

                ////danhSachCoPhieuFull.Testing_TimKiemSuGiongNhauQuaKhu_DanhSachTenCp(danhSachCoPhieu.DanhSachTenCoPhieu, 
                ////    danhSachCoPhieu, arrChuky, danhSachCoPhieu.NgayBatDau, danhSachCoPhieu.NgayKetThuc, 0.9f, 0.4f, 25);

                ////danhSachCoPhieuFull.Testing_TimKiemSuGiongNhauQuaKhu_ToanBoCP(danhSachCoPhieu,
                ////    danhSachCoPhieu.NgayBatDau, danhSachCoPhieu.NgayKetThuc, 0.85f, 15);

                ////danhSachCoPhieuTuongLai.Testing_TimKiemSuGiongNhauQuaKhu_LoadFile(CoreFunctions._pathHOSE_HNX + "Testing_TimKiemSuGiongNhauQuaKhu_ListCP.txt"
                ////    , 6, out listTenCP, out listHeSo);

                ////danhSachCoPhieuTuongLai.Testing_TimKiemSuGiongNhauQuaKhu_LoadFile(CoreFunctions._pathHOSE_HNX + "Testing_TimKiemSuGiongNhauQuaKhu_ToanBoCP.txt"
                ////    , 5, out listTenCP, out listHeSo);

                ////danhSachCoPhieuTuongLai.Testing_TimKiemSuGiongNhauQuaKhu_KiemTraTuongLai(listTenCP, listHeSo);

                ////danhSachCoPhieuFull.Testing_TimKiemSuGiongNhauQuaKhu_ToanBoCP_KiemtraTuongLai(danhSachCoPhieu, danhSachCoPhieuTuongLai,
                ////    danhSachCoPhieu.NgayBatDau, danhSachCoPhieu.NgayKetThuc, 0.95f, 10);
                #endregion

                #region * Testing Tính Volatility
                ////danhSachCoPhieu.Testing_TinhVolatilityGia_KhoiLuong();

                //danhSachCoPhieu.Testing_TinhVolatility();
                #endregion

                #region * Testing Indicators
                //danhSachCoPhieu.Testing_MACD_TinHieuMua(9, 1, 4);
                //danhSachCoPhieu.Testing_MACD_TinHieuMua_KiemTra(9, 1, 4, 1);
                //danhSachCoPhieu.Testing_MACD_TinHieuMua_KiemTra(9, 1, 4, 2);

                ////danhSachCoPhieu.Testing_MFI_MoneyFlowIndex(14);
                ////danhSachCoPhieu.Testing_RSI_RelativeStrengIndex(20);
                //danhSachCoPhieu.Testing_MFI_RSI(14, 20);

                //danhSachCoPhieu.Testing_BollingerBand(20);
                //danhSachCoPhieu.Testing_BollingerBand_KiemTraDoChinhXac(20, 5);

                //danhSachCoPhieu.Testing_PSAR(5);
                //danhSachCoPhieu.Testing_PSAR_KiemTra(5, 5);

                //danhSachCoPhieu.Testing_PSARBollinger_KiemTra(5, 5, 5);
                #endregion

                #region * Testing Big4
                ////List<string> listBig4 = CoreFunctions.File_LoadBig4();

                #endregion

                #region * Testing Full Danh Sách Cổ Phiếu
                //danhSachCoPhieuFull.Testing_TinhPageRankHubAuthority_ZscoreToanBo_Delay(0.9f, 100, 30);
                #endregion

                ///  ----------------- Testing_Excel sẽ thực thi hầu hết các testing khác (testing xong ghi kết quả ra file excel).
                danhSachCoPhieu.Testing_Excel();
                #endregion
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Chart_Market chart = new Chart_Market();
            chart.Show();
        }

        private void btt_modify1Day_Click(object sender, EventArgs e)
        {
            for(int i = 0 ; i < danhSachCoPhieuFull.DanhSachCoPhieu_List.Count;i++)
            {
                if (danhSachCoPhieuFull.DanhSachCoPhieu_List[i].TenCoPhieu == "^VNINDEX")
                {
                    danhSachCoPhieuFull.DanhSachCoPhieu_List.RemoveAt(i);
                    i--;
                }
                else
                    danhSachCoPhieuFull.DanhSachCoPhieu_List[i].Xuly_LoaiBoNgayGiaoDichGanNhat();
            }

            List<string> listContent = new List<string>();
            string text, title = "<Ticker>,<DTYYYYMMDD>,<Open>,<High>,<Low>,<Close>,<Volume>";
            foreach (CoPhieu cp in danhSachCoPhieuFull.DanhSachCoPhieu_List)
            {
                for (int i = 0; i < cp.DanhSachGiaCaoNhat.Count; i++)
                {
                    text = cp.TenCoPhieu + "," + cp.DanhSachNgayGiaoDich_String[i] + "," + cp.DanhSachGiaMoCua[i]
                        + "," + cp.DanhSachGiaCaoNhat[i] + "," + cp.DanhSachGiaThapNhat[i]
                        + "," + cp.DanhSachGiaDongCua[i] + "," + cp.DanhSachKhoiLuongGiaoDich_Int[i];
                    listContent.Add(text);
                }
            }

            listContent.Insert(0, title);
            CoreFunctions.File_WriteAnyFile(System.Windows.Forms.Application.StartupPath + @"\Data\", "MetaStock_HOSE.txt", listContent);
        }

        private void btt_ChartGroup_Click(object sender, EventArgs e)
        {
            danhSachCoPhieu.Testing_MarkovCluster(1, 0.97f);
            Chart_Group chart = new Chart_Group();
            chart.Show();
        }

        private void btt_BackTest_Click(object sender, EventArgs e)
        {
            #region Ghi File BackTest_MyPortfolio
            List<string> listCK = new List<string>();
            for (int i = 0; i < lstbx_DanhMuc.Items.Count; i++)
                listCK.Add(lstbx_DanhMuc.Items[i].ToString());
            CoreFunctions.File_WriteAnyFile(System.Windows.Forms.Application.StartupPath
                + @"\HOSE\BackTest_MyPortfolio.txt", listCK);
            #endregion

            #region Run Python Convex Optimation
            List<float> listAvg = new List<float>();
            List<float> listStd = new List<float>();
            List<string> myPortfolio = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath
                + @"\HOSE\BackTest_MyPortfolio.txt");
            for (int i = 0; i < myPortfolio.Count; i++)
            {
                myPortfolio[i] = myPortfolio[i].Replace("\t", "");
                myPortfolio[i] = myPortfolio[i].Trim();
                if (myPortfolio[i] == "")
                {
                    myPortfolio.RemoveAt(i);
                    i--;
                }
            }
            //danhSachCoPhieu.Testing_Portfolio_ConvexOptimation_Volality(myPortfolio, int.Parse(txtbx_SoNgay_EfficientFrontier.Text)
            //    , ref listAvg, ref listStd);
            //ShareData.Testing_BackTest_Stock_ConvexOptimation(myPortfolio, 60, ref listAvg, ref listStd);
            //ShareData.Testing_BackTest_Stock_ConvexOptimation_Volality(myPortfolio, 60, ref listAvg, ref listStd);

            danhSachCoPhieu.Testing_BackTest_Stock_ConvexOptimation(myPortfolio, int.Parse(txtbx_SoNgay_EfficientFrontier.Text)
                , ref listAvg, ref listStd);
            ShareData.Test_python();
            //danhSachCoPhieu.Testing_Portfolio_MyPortfolio();
            #endregion

            #region LoadFile Tỉ lệ Tối Ưu
            listCK = new List<string>();
            listCK = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath
               + @"\HOSE\Allocation_effFontier.txt");
            cmbx_Reward_ToiUu.Items.Clear();
            for (int i = 0; i < listCK.Count; i++)            
                if (listCK[i] != "")
                {
                    string[] part = listCK[i].Split(new string[] { "\t", "  " }, StringSplitOptions.RemoveEmptyEntries);
                    cmbx_Reward_ToiUu.Items.Add(part[0]);
                }
            

            listCK = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath
                + @"\HOSE\BackTest_MaxAllocationPortfolio.txt");
            lstbx_TileToiUuDanhMuc.Items.Clear();
            for (int i = 0; i < listCK.Count; i++)
            {
                listCK[i] = listCK[i].Replace(":", "\t\t");
                lstbx_TileToiUuDanhMuc.Items.Add(listCK[i]);
            }

            #endregion
        }

        private void ckbx_theodoi_CheckedChanged(object sender, EventArgs e)
        {          
            if (lstbx_xephangtheongay_gia.SelectedItem != null)
            {
                string tencp = "";
                string[] part = lstbx_xephangtheongay_gia.SelectedItem.ToString().Split(new string[] { "\t", " " }, StringSplitOptions.RemoveEmptyEntries);
                if (part.Length > 0)
                {
                    tencp = part[0];
                    if (listCPtheodoi.Contains(tencp) == false && ckbx_theodoi.Checked == true)
                        listCPtheodoi.Add(tencp);
                    if (listCPtheodoi.Contains(tencp) == true && ckbx_theodoi.Checked == false)
                        listCPtheodoi.Remove(tencp);
                    CoreFunctions.File_WriteAnyFile(System.Windows.Forms.Application.StartupPath + @"\Data\CPTheoDoi.txt", listCPtheodoi);
                }
            }
        }
        
        private void btt_Testing_Click(object sender, EventArgs e)
        {
            #region Test
            //ShareData.Testing_BackTest_ADLine();

            //ShareData.Testing_BackTest_HistorgramMACD(danhSachCoPhieu.DanhSachTenCoPhieu);
            //ShareData.Testing_BackTest_HistorgramMACD_SoDinh(danhSachCoPhieu.DanhSachTenCoPhieu);
            //ShareData.Testing_BackTest_HistorgramMACD_SoNgay(danhSachCoPhieu.DanhSachTenCoPhieu);
            //ShareData.Testing_BackTest_MACD_Statistic(danhSachCoPhieu.DanhSachTenCoPhieu);

            //ShareData.Testing_BackTest_HistorgramMACD_Statistic();
            //ShareData.Testing_BackTest_HistorgramMACD_Statistic_Chechlech();
            //ShareData.Testing_BackTest_HistorgramMACD_DieuChinh();
            //ShareData.Testing_BackTest_HistorgramMACD_Statistic_Chechlech_SoNgay();
            //ShareData.Testing_BackTest_HistorgramMACD_Statistic_MauDuLieu();
            //ShareData.Testing_BackTest_HistorgramMACD_XacDinh_SoDinh();

            ///ShareData.Testing_BackTest_HistorgramMACD_DieuChinh();

            ///CoreFunctions.File_ProcessDataStockHOSE();
            #endregion
            List<int> soNgayCachDinhGanNhat = new List<int>();
            List<int> soDinhHistogram = new List<int>();

            //danhSachCoPhieuFull.Testing_Histogram_Cross2EMA_TinHieuMua();
            //danhSachCoPhieuFull.Testing_Cross2EMA_ToiUu();
            //danhSachCoPhieuFull.Testing_Cross2EMA_TinHieu();
            danhSachCoPhieuFull.Testing_MACD_Statistic_TinHieuMua(9, 1, 4);
            //danhSachCoPhieuFull.Testing_AvgRSIMFIIMI_Statistic_Reward(9, 1, 4, ref soNgayCachDinhGanNhat, ref soDinhHistogram, 5);
            
            //ShareData.LoaiboDongDuLieuDu();
            //danhSachCoPhieu.Testing_TinhHubScore(1, 4, float.Parse(txtbx_cluster_threshold.Text));
            //ShareData.Testing_BackTest_HistorgramMACD_Statistic_MauDuLieu();
            //ShareData.Testing_In_SoNgayGiaoDichVNIndex(34);
            ///ShareData.Testing_HienTuongGia();
            //ShareData.Testing_RSI();
            //ShareData.danhsachCoPhieu.Testing_RSI_TatCaCacCP();
            //ShareData.Testing_RSI_TimGanNhat(0.985f);

        }

        private void txtbx_CK_ThemXoa_TextChanged(object sender, EventArgs e)
        {
            this.AcceptButton = btt_ThemXoaCK;
        }

        private void btt_ThemXoaCK_Click(object sender, EventArgs e)
        {
            if (txtbx_CK_ThemXoa.Text != "")
            {
                bool flag_Contain = false;
                string CK = txtbx_CK_ThemXoa.Text.ToUpper();
                for (int i = 0; i < lstbx_DanhMuc.Items.Count; i++)
                    if (lstbx_DanhMuc.Items[i].ToString() == CK)
                    {
                        flag_Contain = true;
                        lstbx_DanhMuc.Items.RemoveAt(i);
                        i--;
                    }
                if (flag_Contain == false)
                    lstbx_DanhMuc.Items.Add(CK);
                txtbx_CK_ThemXoa.Text = "";
            }
        }

        private void cmbx_Reward_ToiUu_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = cmbx_Reward_ToiUu.SelectedIndex;
            List<string> listFile = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath
                + @"\HOSE\Allocation_effFontier.txt");
            string[] part = listFile[index].Split(new string[] { "  ", "\t" }, StringSplitOptions.RemoveEmptyEntries);
            lstbx_TileToiUuDanhMuc.Items.Clear();
            for (int i = 1; i < part.Length; i++)
            {
                part[i] = part[i].Replace(":", "\t\t");
                lstbx_TileToiUuDanhMuc.Items.Add(part[i]);
            }
        }

        private void ckbx_MaxToiUu_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbx_MaxToiUu.Checked == true)
            {
                List<string> listCK = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath
                    + @"\HOSE\BackTest_MaxAllocationPortfolio.txt");

                string row = "";
                for (int i = 0; i < listCK.Count; i++)
                {
                    row += listCK[i] + "  ";
                }
                row = row.Trim();

                listCK = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath
                    + @"\HOSE\Allocation_effFontier.txt");
                int index = -1;
                for(int i = 0 ; i < listCK.Count;i++)
                    if (listCK[i].Contains(row))
                    {
                        index = i;
                        break;
                    }
                if (index >= 0)
                    cmbx_Reward_ToiUu.SelectedIndex = index;
            }
        }


    }
}
